<?php
class ArosController extends AppController{
	var $name = 'Aros';
	var $uses = array('Aco', 'Aro', 'ArosAcos', 'UnionMember', 'Position');
	//var $components = array('ControllerListComponent');
	
	function index(){
		$this->set('aros', $this->Aro->find('all'));
		$this->set('members', $this->UnionMember->find('all'));
		$this->set('positions', $this->Position->find('all'));
		$this->set('curr_user', $this->Session->read('Auth.UnionMember'));
	}
	
	function beforeFilter(){
    	parent::beforeFilter();
    	$this->Auth->allow(array('*'));
    }
	
	function view($id){
		if(!$id){
			$this->Session->setFlash(__('Invalid user'), true);
			$this->Redirect(array('action' => 'index'));
		}
		else{
			$conditions = array('ArosAcos.aro_id' => $id);
			$this->set('aros_acos', $this->ArosAcos->find('all', array('conditions' => $conditions)));
			$this->set('members', $this->UnionMember->find('all'));
			$this->set('positions', $this->Position->find('all'));
			$this->set('acos', $this->Aco->find('all'));
			$acos1 = $this->Aco->query("SELECT Aco.id, Aco.alias, Aco.parent_id, ArosAcos._create FROM Acos as Aco Inner Join aros_acos as ArosAcos ON Aco.id = ArosAcos.aco_id WHERE ArosAcos.aro_id = " . $id . ";");
			$arrayPath = array();
			foreach ($acos1 as $aco1){
				$arrayPath[] = $this->findParents($aco1['Aco']['id']) . $aco1['Aco']['alias'];
			}
			$this->set('path', $arrayPath);
			$this->set('acos1', $acos1);
			$this->set('curr_user', $this->Session->read('Auth.UnionMember'));
			$this->set('aro', $this->Aro->read(null, $id));
		}
	}
	
	function findParents($acoID){
		$result = "";
		$curr_aco = $this->Aco->find('first', array('conditions'=>array('Aco.id' => $acoID)));
		//$result = $curr_aco['Aco']['alias'];
		$acos = $this->Aco->find('all');
		foreach($acos as $aco){
			if(strcmp($curr_aco['Aco']['parent_id'], $aco['Aco']['id']) == 0)
			{
				$result = $aco['Aco']['alias'] . "-" . $result;
				if($aco['Aco']['parent_id'] != NULL)
				{
					$result = $this->findParents($aco['Aco']['id']) . $result;
				}
				break;
			}
				
		}
		return $result;
	}
	
	function add_privilege($id, $aco_id){
		if(!$id){
			$this->Session->setFlash(__('Invalid user'), true);
			$this->Redirect(array('action' => 'index'));
		}
		else{
			$conditions = array('ArosAcos.aro_id' => $id);
			$this->set('aros_acos', $this->ArosAcos->find('all', array('conditions' => $conditions)));
			$this->set('members', $this->UnionMember->find('all'));
			$this->set('positions', $this->Position->find('all'));
			$this->set('acos', $this->Aco->find('all'));
			$acos1 = $this->Aco->query("SELECT Aco.id, Aco.alias, Aco.parent_id " . 
						"FROM acos AS Aco WHERE Aco.id IN(" .
						"SELECT id FROM acos WHERE " .
						"id=" . $aco_id . " OR parent_id=" . $aco_id .
						")");
			$arrayPath = array();
			foreach ($acos1 as $aco1){
				$arrayPath[] = $this->findParents($aco1['Aco']['id']) . $aco1['Aco']['alias'];
			}
			$this->set('path', $arrayPath);
			$this->set('acos1', $acos1);
			$this->set('curr_user', $this->Session->read('Auth.UnionMember'));
			$this->set('aro', $this->Aro->read(null, $id));
		}
	}
	
	function select_controller($id){
		if(!$id){
			$this->Session->setFlash(__('Invalid user'), true);
			$this->Redirect(array('action' => 'index'));
		}
		else{
			$controller = $this->Aco->find('first', array('conditions'=>array('Aco.alias'=>'controllers')));
			$controllers_id = $controller['Aco']['id'];
			$controllers = $this->Aco->find('all', array('conditions'=>array('Aco.parent_id'=>$controllers_id)));
			$this->set('controllers', $controllers);
			$this->set('id', $id);
			$this->set('aro', $this->Aro->read(null, $id));
			$this->set('positions', $this->Position->find('all'));
		}
	}
	
//	function add_privilege2($id){
//		if(!$id){
//			$this->Session->setFlash(__('Invalid user'), true);
//			$this->Redirect(array('action' => 'index'));
//		}
//		else{
//			$positions = $this->Position->read(null, $id);
//			$controllerList = $this->ControllerList->get();
//			$controllerPerms = '';
//			$aco = new Aco();
//			foreach ($controllerList as $controller => $actions){
//				foreach($actions as $key => $action){
//					$controllerPermsp[$controller][$action] = $this->Acl->check($positions, $controller . '/' . $action, '*');
//				}
//			}
//		}
//	}
	
	function getAccessPermission($acoAlias, $aroAlias){
		$aco_path = str_replace("-", "/", $acoAlias);
		//	return $aro_model;
		echo $this->Acl->check($aroAlias, $aco_path);
		exit;
	}
	
	function getPerm($aroModel, $aroForeignKey, $acoAlias){
		$aco_path = str_replace("-", "/", $acoAlias);
		return $this->Acl->check(array('model'=>$aroModel, 'foreign_key'=>$aroForeignKey), $aco_path);
	}
	
	function getAro($id){
		$aro = $this->Aro->find('all', array('conditions'=>array('Aro.id'=>$id)));
		//var_dump($aro);
		$position = $this->Position->find('all', array('conditions'=>array('Position.id'=>$aro[0]['Aro']['foreign_key'])));
		//var_dump($position);
		return $position[0]['Position']['position'];
		
	}
	/*function findChildren($aco_id){
		$acos[] = $this->Acos->find('all', array('conditions'=>array('Acos.parent_id' => $aco_id)));
		
		if(!$acos){
			return null;
		}
		else{
			foreach($acos as $aco){
				$children[] = $this->findChildren($aco['Aco']['id']); 
				if($children != null){
					$i = 0;
					while($children[$i] != null){
						$acos[] = $children[$i];
					}
				}	
			}
		}
		return $acos;
	}*/
}